import std.stdio;
import std.getopt;
import std.format,std.conv;
import imagelib;

/*
  * 本程序的目的是读取文件区分图片文件是gif、jpeg、png
  * @see https://github.com/katsumeshi/image-base64-rs/blob/master/src/lib.rs
  *   if Regex::new(r"^ffd8ffe0").unwrap().is_match(hex) {
  *          return "jpeg"
  *      } else if Regex::new(r"^89504e47").unwrap().is_match(hex) {
  *          return "png"
  *      } else if Regex::new(r"^47494638").unwrap().is_match(hex) {
  *          return "gif"
  *      }
  *
  */

/// main
void main(string[] args)
{
	string imgFileName;
	void verHandler(string option)
	{
		writeln("版本是V0.0.2");
	}

	void imgHandler(string option,string value){
		imgFileName=value;
		auto imgType=imagelib.getImageType(imgFileName);
		writeln(imgType);
		if (imgType!="File does not exist"){
			auto filebody=imagelib.imageEncodeBase64(imgFileName,imgType);
			if (filebody.length>20){
				imagelib.writeBase64(imgFileName,filebody);
			}
		}
	}
	auto helpInformation = getopt(
		args,
		"version|v", "版本号", &verHandler,
		"author"," Sunny", delegate() {writeln("Sunny");return;},
		"file|f", "图像文件名", &imgHandler
		);

	if (helpInformation.helpWanted)
	{
		defaultGetoptPrinter("Some information about the program.", helpInformation.options);
	}
}
